package com.cman777.springc.common.bean.response;

import com.cman777.springc.common.bean.enums.BaseResultCodeEnum;
import com.cman777.springc.common.bean.Iface.ResultCodeEnumI;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;


/**
 * @author chenzhicong
 * @time 2019/10/14 19:49
 * @description json响应的包装
 */
@Getter
@Setter
@ToString
public final class JsonResult<T extends BaseResponse> implements Serializable {


    private String code;


    private String message;


    private T content;

    private static final String OPERATE_FAIL = "操作失败";

    /**
     * 成功
     */
    public JsonResult() {
        this.code = BaseResultCodeEnum.SUCCESS.getCode();
        this.message = BaseResultCodeEnum.SUCCESS.getMessage();
    }

    /**
     * 失败
     * @return
     */
    public JsonResult<T> error() {
        this.code = BaseResultCodeEnum.ERROR.getCode();
        this.message = BaseResultCodeEnum.ERROR.getMessage();
        return this;
    }

    /**
     * 超时
     * @return
     */
    public JsonResult<T> netError() {
        this.code = BaseResultCodeEnum.NET_ERROR.getCode();
        this.message = BaseResultCodeEnum.NET_ERROR.getMessage();
        return this;
    }

    /**
     * 传入响应枚举
     * @return
     */
    public JsonResult<T> result(ResultCodeEnumI resultCodeEnumI) {
        this.code = resultCodeEnumI.getCode();
        this.message = resultCodeEnumI.getMessage();
        return this;
    }

    /**
     * 自定义
     * @return
     */
    public JsonResult<T> result(String code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public JsonResult<T> content(T t){
        this.content = t;
        return this;
    }

    @JsonIgnore
    public boolean isSuccess(){
        return code.equals(BaseResultCodeEnum.SUCCESS.getCode()) ? true : false;
    }





}
